package com.yw.android.letsgo.adapt;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yw.android.letsgo.R;
import com.yw.android.letsgo.bean.res.PetsOnSaleItem;

import java.util.List;

/**
 * ***********************************************************************
 * 项目名称： Yw_BaiduPet
 * 类描述：
 * 包名： com.yw.android.letsgo.adapt
 * 创建人：shanx
 * 创建时间： 2018/5/17 13:21
 * 修改人：shanx
 * 修改时间： 2018/5/17 13:21
 * 修改备注：
 * ***********************************************************************
 *
 * @version 1.0.0
 */
public class SellAdapt extends RecyclerView.Adapter {
    private List<PetsOnSaleItem> mPetsOnSaleItemList;
    private Fragment mFragment;
    //当前滚动的position下面最小的items的临界值
    private int visibleThreshold = 2;
    private LoadMoreDataListener mMoreDataListener;
    private boolean isLoading;

    public SellAdapt(Fragment mFragment, List<PetsOnSaleItem> mPetsOnSaleItemList, RecyclerView mRecyclerView) {
        this.mFragment = mFragment;
        this.mPetsOnSaleItemList = mPetsOnSaleItemList;
        if (mRecyclerView.getLayoutManager() instanceof LinearLayoutManager) {
            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
            //mRecyclerView添加滑动事件监听
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    int totalItemCount = linearLayoutManager.getItemCount();
                    int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                    if (!isLoading && totalItemCount <= (lastVisibleItemPosition + visibleThreshold)) {
                        if (mMoreDataListener != null)
                            mMoreDataListener.loadMoreData();
                        isLoading = true;
                    }
                }
            });
        }
    }

    //加载更多监听方法
    public void setOnMoreDataLoadListener(LoadMoreDataListener onMoreDataLoadListener) {
        mMoreDataListener = onMoreDataLoadListener;
    }

    public void setLoaded() {
        isLoading = false;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SellViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_sell_dog, null));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        SellViewHolder mSellViewHolder = (SellViewHolder) holder;
        PetsOnSaleItem mPetsOnSaleItem = mPetsOnSaleItemList.get(position);
        mSellViewHolder.tv.setText(mPetsOnSaleItem.amount);
        Glide.with(mFragment)
                .load(mPetsOnSaleItem.petUrl)
                .into(mSellViewHolder.iv);
    }

    @Override
    public int getItemCount() {
        return mPetsOnSaleItemList.size();
    }

   public interface LoadMoreDataListener {
        public void loadMoreData();
    }

    class SellViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv;
        public TextView tv;

        public SellViewHolder(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}
